What is jquery?
The jQuery npm package is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, and animation much simpler with an easy-to-use API that works across a multitude of browsers. With a combination of versatility and extensibility, jQuery has changed the way that millions of people write JavaScript.
What are jquery's main functionalities?
DOM Traversal and Manipulation
Easily select and manipulate HTML elements. This code changes the text of all paragraph elements to 'Hello World'.
$( 'p' ).text( 'Hello World' );
Event Handling
Quickly handle events like clicks. This code sets up an alert to be displayed when any button is clicked.
$( 'button' ).click( function() { alert( 'Button clicked!' ); } );
Animation
Perform animations on elements. This code makes a div element slide up slowly.
$( 'div' ).slideUp( 'slow' );
AJAX
Facilitate asynchronous HTTP (Ajax) requests. This code fetches the content of test.html and inserts it into the body of the current page.
$.ajax({ url: 'test.html', success: function(data) { $( 'body' ).html( data ); } });
Other packages similar to jquery
zepto
Zepto is a minimalist JavaScript library for modern browsers with a largely jQuery-compatible API. It's smaller in size compared to jQuery but does not support as many browsers.
cash
Cash is an absurdly small jQuery alternative for modern browsers. It provides jQuery-style syntax for manipulating the DOM, handling events, and making AJAX requests, but with a smaller footprint.
mootools
MooTools is a collection of JavaScript utilities designed for the intermediate to advanced JavaScript developer. It allows you to write powerful and flexible code with its elegant, well documented, and coherent API. MooTools code is extensively documented and easy to read, which is a strong point compared to jQuery.
jQuery
jQuery is a fast, small, and feature-rich JavaScript library.
For information on how to get started and how to use jQuery, please see jQuery's documentation.
For source files and issues, please visit the jQuery repo.
Including jQuery
Below are some of the most common ways to include jQuery.
Browser
Script tag
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
Babel
Babel is a next generation JavaScript compiler. One of the features is the ability to use ES6/ES2015 modules now, even though browsers do not yet support this feature natively.
import $ from "jquery";
Browserify/Webpack
There are several ways to use Browserify and Webpack. For more information on using these tools, please refer to the corresponding project's documention. In the script, including jQuery will usually look like this...
var $ = require("jquery");
AMD (Asynchronous Module Definition)
AMD is a module format built for the browser. For more information, we recommend require.js' documentation.
define(["jquery"], function($) {
});
Node
To include jQuery in Node, first install with npm.
npm install jquery
For jQuery to work in Node, a window with a document is required. Since no such window exists natively in Node, one can be mocked by tools such as jsdom. This can be useful for testing purposes.
require("jsdom").env("", function(err, window) {
if (err) {
console.error(err);
return;
}
var $ = require("jquery")(window);
});